/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { constants } from "./constants.js";
import { isRunning } from "./helper.js";
import { Node } from "./Node.js";
"use strict";

var __importDefault = this && this.__importDefault || function (mod) {
    return mod && mod.__esModule ? mod : { "default": mod };
};

const constants_1 = constants;
const Node_1 = __importDefault(Node);
class BranchNode extends Node_1.default {
    constructor(blueprint) {
        super(blueprint);
        // Override this in subclasses
        this.OPT_OUT_CASE = constants_1.SUCCESS;
        this.START_CASE = constants_1.SUCCESS;
        this.nodeType = 'BranchNode';
        this.nodes = blueprint.nodes || [];
        this.numNodes = this.nodes.length;
    }
    run(blackboard = {}, { lastRun, introspector, rerun, registryLookUp = x => x } = {}) {
        if (!rerun) this.blueprint.start(blackboard);
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let overallResult = this.START_CASE;
        const results = [];
        const lastRunStates = typeof lastRun === 'object' && lastRun.state || [];
        const startingIndex = Math.max(lastRunStates.findIndex(x => (0, isRunning)(x)), 0);
        let currentIndex = 0;
        for (; currentIndex < this.numNodes; ++currentIndex) {
            if (currentIndex < startingIndex) {
                // Keep last result
                results[currentIndex] = lastRunStates[currentIndex];
                continue;
            }
            const node = registryLookUp(this.nodes[currentIndex]);
            const result = node.run(blackboard, { lastRun: lastRunStates[currentIndex], introspector, rerun, registryLookUp });
            results[currentIndex] = result;
            if (result === constants_1.RUNNING || typeof result === 'object') {
                overallResult = constants_1.RUNNING;
                break;
            } else if (result === this.OPT_OUT_CASE) {
                overallResult = result;
                break;
            } else {
                rerun = false;
            }
        }
        const running = (0, isRunning)(overallResult);
        if (!running) {
            this.blueprint.end(blackboard);
        }
        if (introspector) {
            const debugResult = running ? constants_1.RUNNING : overallResult;
            introspector.wrapLast(Math.min(currentIndex + 1, this.numNodes), this, debugResult, blackboard);
        }
        return overallResult === constants_1.RUNNING ? { total: overallResult, state: results } : overallResult;
    }
}
// exports.default = BranchNode;
export { BranchNode }